package main

import (
	"fmt"
	"github.com/gorilla/mux"
	"log"
	"net/http"
	"time"
)

func WebSocketHandler(w http.ResponseWriter, r *http.Request) {
	hj, ok := w.(http.Hijacker)
	if !ok {
		http.Error(w, "Hijacking not supported", http.StatusInternalServerError)
		return
	}

	conn, _, err := hj.Hijack()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	defer conn.Close()

	// 在这里处理WebSocket连接
	// 你可以根据需要进行读取消息、处理消息和发送消息的操作
}

func main() {

	r := mux.NewRouter()

	// 注册WebSocket处理函数
	r.HandleFunc("/ws", WebSocketHandler)

	// 启动服务器
	log.Println("Starting server on :8080")
	err := http.ListenAndServe(":18182", r)
	if err != nil {
		log.Fatal(err)
	}
}

func handleMessage(message string) {
	currentTime := time.Now()
	timeString := currentTime.Format("2006-01-02 15:04:05")

	fmt.Println(timeString, ":Received message:", message)
}
